package com.njtoyo.taxi.admin.library.taxi_service;

import com.njtoyo.taxi.admin.library.common.Enum;
import com.njtoyo.taxi.admin.mapper.master.entity.User;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.system_message.DriverAuditSendMsgWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@Slf4j
@Service
public class TaxiService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${config.taxiService.taxiUrl}")
    private String baseUrl;

    @Async
    public void pushDriverNotifyMessage(Long messageId) {
        String url = baseUrl + "/ha/send/driver/notify/msg/topic";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("messageId", messageId);

        log.info("司机通知消息请求发送topic接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("司机通知消息请求发送topic接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("司机通知消息，请求外部接口失败: {}", e.getMessage());
            e.printStackTrace();
        }
    }

    // 重新计算并更新司机分数（同时更新缓存与数据库）
    @Async
    public void refreshDriverScore(Long driverId) {
        String url = baseUrl + "/ha/driver/score?id=" + driverId;

        log.info("重新计算并更新司机分数, {}, {}", url, driverId);
        try {
            TaxiServiceResponse res = restTemplate.getForObject(url, TaxiServiceResponse.class);
            log.info("重新计算并更新司机分数接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("重新计算并更新司机分数，请求外部接口失败: {}", e.getMessage());
        }
    }

    @Async
    public void updateDriverStateNotice(Long id, Enum.DriverState state) {
        String url = baseUrl + "/ha/send/driver/state";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("driverId", id);
        request.put("state", state.name());

        log.info("修改司机状态通知请求发送接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("修改司机状态通知请求发送接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("修改司机状态通知请求发送接口，请求外部接口失败: {}", e.getMessage());
            e.printStackTrace();
        }
    }

    @Async
    public void sendSmsMessage(Enum.SmsMsgCategory category, String mobileNumber) {
        String url = baseUrl + "/ha/send/sms/msg";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("category", category);
        request.put("mobileNumber", mobileNumber);

        log.info("发送短信消息接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("发送短信消息接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("发送短信消息，请求外部接口失败: {}", e.getMessage());
            e.printStackTrace();
        }
    }

    @Async
    public void clearCustomerCache(User user) {
        String url = baseUrl + "/ha/cache/clear_customer?id=" + user.getId();

        log.info("清除乘客缓存接口, {}", url);
        try {
            TaxiServiceResponse res = restTemplate.getForObject(url, TaxiServiceResponse.class);
            log.info("清除乘客缓存接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("清除乘客缓存，请求外部接口失败: {}", e.getMessage());
            e.printStackTrace();
        }
    }

    /*
      行程解绑隐私通话
     */
    @Async
    public void unboundNumber(Long rideId) {
        String url = baseUrl + "/ha/ride/unbound/number";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("rideId", rideId);

        log.info("行程解绑隐私通话接口, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("行程解绑隐私通话 接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("行程解绑隐私通话，请求外部接口失败: {}", e.getMessage());
            e.printStackTrace();
        }
    }

    /*
      修改ride状态后给各端的消息通知
     */
    @Async
    public void updateRideStateNotice(Long rideId, String stateStr) {
        String url = baseUrl + "/ha/notice/ride";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("rideId", rideId);
        request.put("state", stateStr);

        log.info("修改行程状态下发通知, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("修改行程状态下发通知 接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("修改行程状态下发通知，请求外部接口失败: {}", e.getMessage());
            e.printStackTrace();
        }
    }

    /*
      关闭司机的自动抢单后给各端的消息通知
     */
    @Async
    public void closeDriverAutomaticOpenNotice(Long driverId) {
        String url = baseUrl + "/ha/send/msg/violation";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("driverId", driverId);

        log.info("关闭司机自动抢单通知, {}, {}", url, request);
        try {
            TaxiServiceResponse res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("关闭司机自动抢单通知 接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("关闭司机自动抢单通知，请求外部接口失败: {}", e.getMessage());
            e.printStackTrace();
        }
    }

    // 清除司机缓存
    @Async
    public void clearDriverCache(Long driverId) {
        String url = baseUrl + "/ha/cache/clear_driver?id=" + driverId;

        log.info("清除司机缓存, {}, {}", url, driverId);
        try {
            TaxiServiceResponse res = restTemplate.getForObject(url, TaxiServiceResponse.class);
            log.info("清除司机缓存 接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("清除司机缓存，请求外部接口失败: {}", e.getMessage());
        }
    }

    // 新乘客激活给推荐人发奖励
    public TaxiServiceResponse compensateCustomerRecommender(String mobileNumber) {
        String url = baseUrl + "/ha/compensate/recommender";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("mobileNumber", mobileNumber);

        log.info("新乘客激活给推荐人发奖励 接口, {}, {}", url, request);
        TaxiServiceResponse res = new TaxiServiceResponse();

        try {
            res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("新乘客激活给推荐人发奖励 接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("新乘客激活给推荐人发奖励 接口失败: {}", e.getMessage());
            e.printStackTrace();
        }

        return res;
    }

    // 乘客首单给推荐人发奖励
    public TaxiServiceResponse compensateCustomerFirstRide(String mobileNumber) {
        String url = baseUrl + "/ha/compensate/first/ride";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("mobileNumber", mobileNumber);

        log.info("乘客首单给推荐人发奖励 接口, {}, {}", url, request);
        TaxiServiceResponse res = new TaxiServiceResponse();

        try {
            res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("乘客首单给推荐人发奖励 接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("乘客首单给推荐人发奖励 接口失败: {}", e.getMessage());
            e.printStackTrace();
        }

        return res;
    }

    public TaxiServiceResponse sendPushMsg(DriverAuditSendMsgWrapper req) {
        String url = baseUrl + "/ha/send/push/msg";
        HashMap<Object, Object> request = new HashMap<>();
        request.put("id", req.getId());
        request.put("useGroup", req.getUseGroup());
        request.put("title", req.getTitle());
        request.put("content", req.getContent());

        log.info("发送push 接口, {}, {}", url, request);
        TaxiServiceResponse res = new TaxiServiceResponse();

        try {
            res = restTemplate.postForObject(url, request, TaxiServiceResponse.class);
            log.info("发送push 接口返回：{}", res);
        } catch (RestClientException e) {
            log.info("发送push 接口失败: {}", e.getMessage());
            e.printStackTrace();
        }

        return res;
    }

    public TaxiServiceResponse refreshRegion() {
        String url = baseUrl + "/ha/refresh/region";
        return restTemplate.getForObject(url, TaxiServiceResponse.class);
    }
}
